<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="searchValue"
      ></el-input>
      <el-button @click="clearSearch">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="edit(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="del(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="currentChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="addDialog">+ 新增</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      title="提示"
      :visible="addVisible"
      width="30%"
      @close="closeAddDialog"
    >
      <template #default>
        <el-form
          ref="addForm"
          label-width="120px"
          :model="addList"
          :rules="rules"
        >
          <el-form-item label="任务名称" prop="name">
            <el-input
              v-model="addList.name"
              style="width: 80%"
              placeholder="1-50个字符"
            />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input
              v-model="addList.des"
              style="width: 80%"
              placeholder="1-50个字符"
            />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="addSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import {
  gettaskInfo,
  deltaskInfo,
  searchtaskInfo,
  addtaskInfo,
  edittaskInfo,
  gettaskInfoDetail,
} from "@/api/task.js";
export default {
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      searchValue: "",
      addVisible: false,
      // 双向绑定
      addList: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "任务名称要求1-50个字符",
            trigger: "blur",
          },
        ],
        des: [
          { required: true, message: "任务描述不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "任务描述要求1-50个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await gettaskInfo(this.params);
      console.log(res);
      this.list = res.data;
      this.total = +res.headers["x-total-count"];
      console.log(this.list);
    },
    // 分页器页码
    currentChange(page) {
      this.params._page = page;
      this.getList();
    },
    // 删除
    async del(id) {
      await deltaskInfo(id);
      this.getList();
      this.$message.success("删除成功");
    },
    // 搜索
    async search() {
      const res = await searchtaskInfo(this.searchValue);
      // console.log(res.data);
      this.list = res.data;
    },
    async clearSearch() {
      this.searchValue = "";
      this.getList();
    },
    // 点击新增键
    async addDialog() {
      this.addVisible = true;
    },
    // 关闭弹框
    closeAddDialog() {
      this.addVisible = false;
      this.$refs.addForm.resetFields();
    },
    // 兜底校验
    addSubmit() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.addList.id) {
            // 调用编辑接口
            await edittaskInfo(this.addList);
            this.$message.success("编辑任务成功");
          } else {
            // 调用新增接口
            this.addList.id = uuidv4();
            await addtaskInfo(this.addList);
            this.$message.success("新增任务成功");
          }

          this.addVisible = false;
          // 重新渲染数据
          this.getList();
          // 重置表单(重置还有清除校验痕迹的作用 要保留)
          this.$refs.addForm.resetFields();
          // 重置表单
          this.addList = {
            name: "",
            des: "",
            id: "",
          };
        }
      });
    },
    // 编辑
    async edit(id) {
      this.addVisible = true;
      const res = await gettaskInfoDetail(id);
      this.addList = res.data;
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
